/**
 * NwClient.java
 * - Establish server connections
 * - Sending Objects
 * - Receiving Objects
 * - Closing server connections
 *
 * @author:  John Thomason
 *  Course:  CS223
 *  Assign:  2
 * Seminar:  6
 *    Date:  Feb 15, 2009
 */

// Assigned Package
package trader.nw;

// Package Imports
import java.net.*;
import java.io.*;

/**
 * NwClient Class
 */
public class NwClient
{
    //Instance Variables
    String             host;
    int                port;
    Socket             skt;
    InputStream        is;
    OutputStream       os;
    ObjectInputStream  ois;
    ObjectOutputStream oos;


    //Constructors
    public NwClient(){
        this("127.0.0.1", 5280);
    }
    public NwClient(String host, int port){
        this.host = host;
        this.port = port;
        connect();
    }//NwClient Constructor

    /**
     * Make a client connection to the server,
     * using the previously assigned host and port values.
     */
    public void connect(){
        try {
            skt = new Socket(host,port);
            is  = skt.getInputStream();
            ois = null;
            os  = skt.getOutputStream();
            oos = new ObjectOutputStream(os);
        }//try
        catch (Exception e){
            System.out.println("NwClient.connect(): " + e.toString());
        }//catch
    }//connect()

    /**
     * Make a client connection to the server,
     * using the passed host and port values.
     *
     * @param host  The host running the server
     * @param port  The port the server is listening
     */
    public void connect(String host, int port){
        this.host = host;
        this.port = port;
        connect();
    }//connect(String,int)

    /**
     * Send an object to the server.
     *
     * @param obj   The Object to send
     * @throws java.lang.Exception
     */
    public void send(Object obj) throws Exception {
        try {
            oos.writeObject(obj);
        }//try
        catch (Exception e){
            throw new Exception("NwClient.send():\n", e);
        }//catch
    }//send(Object)

    /**
     * Receive an object from the server.
     *
     * @return  The received object.
     * @throws java.lang.Exception
     */
    public Object receive() throws Exception {
        Object obj = null;
        try {
            if (ois == null && 
                 is != null)
                ois = new ObjectInputStream(is);
            obj = ois.readObject();
        }//try
        catch (Exception e){
            throw new Exception("NwClient.receive():\n", e);
        }
        return obj;
    }//receive()

    /**
     * Close the current connection (socket) to the server.
     */
    public void close(){
        try {
            if (skt != null){
                skt.close();
                is  = null;
                ois = null;
                os  = null;
                oos = null;
            }//if
        }//try
        catch (Exception e){
            System.out.println("NwClient.close():\n" + e.getMessage());
        }//catch
    }//close()

}//NwClient Class
